package com.bookmanager.www.service.impl;

import cn.hutool.core.lang.ObjectId;
import com.bookmanager.www.entity.FlAddress;
import com.bookmanager.www.mapper.FlAddressMapper;
import com.bookmanager.www.service.FlAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;

/**
 * <p>
 * 用户的地址表（用户与用户地址表对应关系应该是一对多) 服务实现类
 * </p>
 *
 * @author 风离
 * @since 2021-09-17
 */
@Service
public class FlAddressServiceImpl extends ServiceImpl<FlAddressMapper, FlAddress> implements FlAddressService {

    @Resource
    FlAddressMapper flAddressMapper;
    @Override
    public ArrayList addAddress(String userId, String name, String phoneNum, Integer addressDefault, String receiveProvince,
                                String receiveCity, String receiveCounty, String receiveDesc) {
        //生成唯一地址Id
        String addressId = ObjectId.next();

        /**
         * 其他设为非默认
         */
        if(addressDefault==1)
        {
            int update = flAddressMapper.updateOtherAddressDefault(addressId,userId);
            System.out.println("修改了几个默认地址？？"+update);
        }

        LocalDateTime createdTimestamp = LocalDateTime.now();
        LocalDateTime updatedTimestamp = LocalDateTime.now();
        //默认刚创建地址时无订单情况
        String orderId ="";
       ArrayList arrayList =new ArrayList();
       arrayList.add(0,flAddressMapper.addAddress(addressId,
               userId,name,phoneNum,addressDefault,receiveProvince,
               receiveCity,receiveCounty,receiveDesc,createdTimestamp,updatedTimestamp,
               orderId
       ));
       arrayList.add(1,addressId);

        return arrayList;
    }
}
